<?php
/*
$mutex = Mutex::create();
if($mutex){
   exit;
   Mutex::destroy($mutex);	
}*/

if ( !defined("SCRIPT_DIR") ) {
	define("SCRIPT_DIR", dirname(__FILE__));
}
$path = explode('/',__FILE__);
if ( !defined("FILE") ) {
	define("FILE",end($path));
}


function path_format($file) {
	return SCRIPT_DIR . '/' . $file;
}
include_once(path_format('logger.php'));
include_once(path_format('config.php'));
include_once(path_format('mysql.php'));
include_once(path_format('redis.php'));

function sync() {
	$preTenMinTime = 0;
	while(1)
	{
		
        echo date('Y:m:d H:i:s')." ZjhSyncStatisticsOnline working...\r\n";
 		$now = time();
		$curTenMinTime = (int) ($now/600) * 600;
		if ($curTenMinTime > $preTenMinTime)
		{
			$preTenMinTime = $curTenMinTime;
			//do some on time thing
			
			//连接数据库
			$mysql = new MysqlDriver();
			$ret = $mysql->connect(Config::$mysql_config);
			if ( !$ret ) {
				Config::$log->error(FILE, "connect mysql error.");
			}
			//连接redis			
			$redis = new RedisDriver();
			$ret = $redis->initDataRedis(5);
			if (!$ret){
				Config::$log->error(FILE, "initCacheRedis error");
			}
		
			$onlineInfo = $redis->data_redis->hgetAll("honline");	
			$venueids = $redis->data_redis->lRange("lv:ids", 0, -1);
			$venues = array();
			foreach ($venueids as $vid) {			
				$vname = "velue" . $vid;			 
			    $info =array();
			    $info['vid'] = $vid;
			    $info['timestamp'] = $curTenMinTime;
			    $info['amount'] = (int)$onlineInfo["$vname"];
			    $where = array();
			    $result = $mysql->insert("onplay",$info);
			}						
		}
		
		sleep(60);

	}
	
	
}

sync();
